$(function() {
	$('#loading').ajaxStart(function(){
		var pageScroll = $.wis.common.getPageScroll();
		var pageSize   = $.wis.common.getPageSize();
		$('#loading').css({top: pageScroll[1]+5, left: pageSize[0]-$('#loading').width()-25});
		$(this).fadeIn();
	}).ajaxStop(function(){
		$(this).fadeOut();
	});
});

var flipDirectionArr = new Array('tb', 'bt');
function getFlipDirection(){
	return flipDirectionArr[Math.round(Math.random()*1)];
}


(function($) {
	if(!$.wis)
		$.wis=function(){};
		
	$.wis.util={
		settings : {
			mainContent	: '#mainContent',
			menuContent	: '#menuContent',
			pagerId		: '#pagination',
			navigator 	: '#pageNavigator',
			mainForm 	: '#mainForm',
			editForm	: '#editForm'
		},
		
		doView:function(url){
			$(document).ready(function($) {
				$.ajax({
					type	: 'POST',
					url		: url,
					data	: '',
					success	: function(data) {
						if(data.length>8 && data.substring(0,8)=='[error]:'){
							$.modalbox.showError(data.substring(8,data.length));
						}else{
							var s = $.wis.util.settings;
							//$(s.mainContent).html(data);
							//$.pagination.create(s.pagerId, s.navigator, s.mainForm, s.mainContent);
							$(s.mainContent).flip({ 
								direction: getFlipDirection(),
								color: '#EEEEEE',
								speed:500, 
								content: data,
								onEnd: function(){$.pagination.create(s.pagerId, s.navigator, s.mainForm, s.mainContent)}
							});
						}
					},
					error : function(data) {
						$.modalbox.showError("Get Server Error :: "+data.status);
					}
				})	
			});
		},
		
		doView1:function(url){
			$(document).ready(function($) {
				$.ajax({
					type	: 'POST',
					url		: url,
					data	: '',
					success	: function(data) {
						if(data.length>8 && data.substring(0,8)=='[error]:'){
							$.modalbox.showError(data.substring(8,data.length));
						}else{
							var s = $.wis.util.settings;
							//$(s.mainContent).html(data);
							$(s.mainContent).flip({ 
								direction: getFlipDirection(),
								color: '#EEEEEE',
								speed:500, 
								content: data
							});
						}
					},
					error : function(data) {
						$.modalbox.showError("Get Server Error :: "+data.status);
					}
				})	
			});
		},
		
		doEdit:function(id){
			var s = $.wis.util.settings;
			var url = $(s.mainForm).attr('action')+'.edit';
			$.facebox.loading(true);
			$($.facebox.settings.faceboxId+' .title').text(id?"Edit":"New");
			$.ajax({
				type	: $(s.mainForm).attr('method').toUpperCase(),
				url		: url+(id?'?id='+id:''),
				data	: $(s.mainForm).serialize(),
				success	: function(data) {					
					if(data.length>8 && data.substring(0,8)=='[error]:'){
						$.facebox.close();
						$.modalbox.showError(data.substring(8,data.length));
					}else{
						$.facebox.reveal(data, $.facebox.settings.faceboxId);
					}
				},
				error : function(data) {
					$.modalbox.showError("Get Server Error :: "+data.status);
				}
			})
			return false;
		},
		
		doViewForm:function(url){
			var s = $.wis.util.settings;
			if(!url)url = $(s.mainForm).attr('action')+'.form';
			$.facebox.loading(true);
			$($.facebox.settings.faceboxId+' .title').text("Add");
			$.ajax({
				type	: "POST",
				url		: url,
				data	: '',
				success	: function(data) {					
					if(data.length>8 && data.substring(0,8)=='[error]:'){
						$.facebox.close();						
						$.modalbox.showError(data.substring(8,data.length));
					}else{
						$.facebox.reveal(data, $.facebox.settings.faceboxId);
					}
				},
				error : function(data) {
					$.modalbox.showError("Get Server Error :: "+data.status);
				}
			})
			return false;
		},
		
		doDel:function(id){			
			var s = $.wis.util.settings;
			var url = $(s.mainForm).attr('action')+'.del';
			if(id){
				$(s.mainForm).append('<input type="hidden" name="id" id="id" value="'+id+'">')
			}
			$.ajax({
				type	: $(s.mainForm).attr('method').toUpperCase(),
				url		: url,
				data	: $(s.mainForm).serialize(),
				success	: function(data) {
					if(data.length>8 && data.substring(0,8)=='[error]:'){						
						$(s.mainForm).children('#id').remove();
						$.modalbox.showError(data.substring(8,data.length));
					}else{
						$(s.mainContent).html(data);
						$.pagination.create(s.pagerId, s.navigator, s.mainForm, s.mainContent);
					}						
				},
				error : function(data) {
					$(s.mainForm).children('#id').remove();
					$.modalbox.showError("Get Server Error :: "+data.status);
				}
			})
			return false;
		},
		
		doSave:function(){
			var s = $.wis.util.settings;
			$.ajax({
				type	: $(s.editForm).attr('method').toUpperCase(),
				url		: $(s.editForm).attr('action'),
				data	: $(s.editForm).serialize(),
				success	: function(data) {	
					$.facebox.close();						
					if(data.length>10 && data.substring(0,10)=='[warning]:'){
						$.modalbox.showWarning(data.substring(10,data.length));
					}else if(data.length>8 && data.substring(0,8)=='[error]:'){
						$.modalbox.showError(data.substring(8,data.length));
					}else{
						//$(s.mainContent).html(data);
						//$.pagination.create(s.pagerId, s.navigator, s.mainForm, s.mainContent);						
						$(s.mainContent).flip({ 
							direction: getFlipDirection(),
							color: '#EEEEEE',
							speed:500, 
							content: data,
							onEnd: function(){$.pagination.create(s.pagerId, s.navigator, s.mainForm, s.mainContent)}
						});
					}
				},
				error : function(data) {
					$.modalbox.showError("Get Server Error :: "+data.status);
				}
			})
			return false;
		},

		doSave1:function(){
			var s = $.wis.util.settings;
			$.ajax({
				type	: $(s.editForm).attr('method').toUpperCase(),
				url		: $(s.editForm).attr('action'),
				data	: $(s.editForm).serialize(),
				success	: function(data) {	
					$.facebox.close();						
					if(data.length>10 && data.substring(0,10)=='[warning]:'){
						$.modalbox.showWarning(data.substring(10,data.length));
					}else if(data.length>8 && data.substring(0,8)=='[error]:'){
						$.modalbox.showError(data.substring(8,data.length));
					}else{
						//$(s.mainContent).html(data);
						$(s.mainContent).flip({ 
							direction: getFlipDirection(),
							color: '#EEEEEE',
							speed:500, 
							content: data
						});
					}
				},
				error : function(data) {
					$.modalbox.showError("Get Server Error :: "+data.status);
				}
			})
			return false;
		},

		doCheck:function(){
			var all = document.getElementById('selectAllId');
			if(!all) return;		
			var ids = $("input[name='selectedRecord']");
			if(ids && ids.length>0){
				for(i=0; i<ids.length; i++)
					ids[i].checked = all.checked;
			}
		},
		
		doRenderContent:function(url, idDivContent){
			$(document).ready(function($) {
				$.ajax({
					type	: 'POST',
					url		: url,
					data	: '',
					success	: function(data) {
						if(data.length>10 && data.substring(0,10)=='[warning]:'){
							return;
						}else if(data.length>8 && data.substring(0,8)=='[error]:'){
							return;
						}else{
							$(idDivContent).html(data);
						}
					},
					error : function(data) {
						$.modalbox.showError("Get Server Error :: "+data.status);
					}
				})	
			});
		},

		doSortDisplayTag:function(qryStrDT){		
			var s = $.wis.util.settings;
			var url = $(s.mainForm).attr('action')+'.view';
			
			//alert("url == "+url);
			queryArrDT = qryStrDT.split("&");
			for (i=0; i<queryArrDT.length; i++){
				ft = queryArrDT[i].split("=");
				
				$(s.mainForm).children("#"+ft[0]).remove();
				$(s.mainForm).append('<input type="hidden" name="'+ft[0]+'" id="paramDT" value="'+ft[1]+'">')
			}
			
			$.ajax({
				type	: $(s.mainForm).attr('method').toUpperCase(),
				url		: url,
				data	: $(s.mainForm).serialize(),
				success	: function(data) {
					if(data.length>8 && data.substring(0,8)=='[error]:'){						
						$(s.mainForm).children('#paramDT').remove();
						$.modalbox.showError(data.substring(8,data.length));
					}else{
						//$(s.mainContent).html(data);
						//$.pagination.create(s.pagerId, s.navigator, s.mainForm, s.mainContent);
						$(s.mainContent).flip({ 
							direction: getFlipDirection(),
							color: '#EEEEEE',
							speed:500, 
							content: data,
							onEnd: function(){$.pagination.create(s.pagerId, s.navigator, s.mainForm, s.mainContent)}
						});
					}						
				},
				error : function(data) {
					$(s.mainForm).children('#paramDT').remove();
					$.modalbox.showError("Get Server Error :: "+data.status);
				}
			})
			return false;
		}
	};
})(jQuery);